<?php
session_start();

require_once("../config/include_tokyo.inc.php");


//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 ||  $userRole == 5){
	//管理员,产品管理员,任务管理员+产品管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}



//更新产品内容
$doFlag = $_POST["doFlag"];
if($doFlag == "updateProduct"){
	$productId = $_POST["productId"];
	$_GET['id'] = $productId;
	$image_name = $_POST["image_name"];
	$factory_name = $_POST["factory_name"];
	$wavelength = $_POST["wavelength"];
	$power = $_POST["power"];
	$price = $_POST["price"];
	$note = $_POST["note"];
	$type_id = $_POST["type_id"];
	$order_id = $_POST["order_id"];
	$use_flag = $_POST["use_flag"];


	/**
	*处理排序ID，排在编号xxx的后面，默认排在最后面
	*例如排在3的后面，那么当前编号是4，原来的从4开始的编号都加1
	**/
	$sql = "select order_id from factoryproducts WHERE id=".$productId;
	$result = $db->Execute($sql);
	if(!$result->EOF){
			$current_order_id = $result->fields['order_id'];
	}
	if($current_order_id != $order_id){//更新时，排序id没变化 就不更新；
		if($order_id == ""){
			$sqlMaxId = "select max(order_id) as order_id from factoryproducts WHERE type_id=".$type_id;
			$result = $db->Execute($sqlMaxId);
			if(!$result->EOF){
				$maxId = $result->fields['order_id'];
				$order_id = $maxId + 1;
			}
		}else{
			$sqlUpdate = "update factoryproducts  set order_id = (order_id + 1) where order_id > ".$order_id." and type_id=".$type_id;
			$order_id = $order_id + 1;
			$db->Execute($sqlUpdate);	
		}
	}

	
	
	$sql = "update factoryproducts set image_name = '".$image_name."'";
	$sql = $sql.", factory_name='".$factory_name."'";
	$sql = $sql.", wavelength='".$wavelength."'";
	$sql = $sql.", power='".$power."'";
	$sql = $sql.", price='".$price."'";
	$sql = $sql.", note='".$note."'";
	$sql = $sql.", type_id=".$type_id;
	$sql = $sql.", order_id=".$order_id;
	$sql = $sql.", use_flag=".$use_flag;
	$sql = $sql."  where id=".$productId;	
	$db->Execute($sql);


	
}else if($doFlag == "insertProduct"){
	//保存 并返回到列表
	$image_name = $_POST["image_name"];
	$factory_name = $_POST["factory_name"];
	$wavelength = $_POST["wavelength"];
	$power = $_POST["power"];
	$price = $_POST["price"];
	$note = $_POST["note"];
	$type_id = $_POST["type_id"];
	$order_id = $_POST["order_id"];
	$use_flag = $_POST["use_flag"];

	/**
	*处理排序ID，排在编号xxx的后面，默认排在最后面
	*例如排在3的后面，那么当前编号是4，原来的从4开始的编号都加1
	**/
	if($order_id == ""){
		$sqlMaxId = "select max(order_id) as order_id from factoryproducts WHERE type_id=".$type_id;
		$result = $db->Execute($sqlMaxId);
		if(!$result->EOF){
			$maxId = $result->fields['order_id'];
			$order_id = $maxId + 1;
		}
	}else{
		$sqlUpdate = "update factoryproducts  set order_id = (order_id + 1) where order_id > ".$order_id." and type_id=".$type_id;
		$order_id = $order_id + 1;
		$db->Execute($sqlUpdate);	
	}
	
	$sql = "INSERT INTO factoryproducts
		(image_name,factory_name,wavelength,power,price,note,type_id,order_id,use_flag) 
		values 
  		('$image_name','$factory_name','$wavelength','$power','$price','$note',$type_id,$order_id,$use_flag)";
	$db->Execute($sql);
	
}else if($doFlag == "deleteProduct"){
	$productId = $_POST["productId"];
	$sql = "delete from factoryproducts where id=".$productId;
	$db->Execute($sql);
}

//产品详情 
$id = $_GET['id'];
if($id == ""){
	//新增产品
}else{
	//编辑产品
	$sql = "SELECT * from factoryproducts where id=".$id;
	$result = $db->Execute($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "产品详情"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="inputForm" action="../factory/productInput.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>产品详情:
<?php  if($doFlag == "updateProduct"){echo "产品内容更新成功!";}?>
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
if($id == ""){
	$strTR = "";
	$strTR = $strTR."<tr ><td>产品图片</td>";
	$strTR = $strTR."<td><input type='text' name='image_name' id='image_name'></td></tr>";
	$strTR = $strTR."<tr ><td>工厂名称</td>";
	$strTR = $strTR."<td><input type='text' name='factory_name' id='factory_name'></td></tr>";
	$strTR = $strTR."<tr ><td>波长(nm)</td>";
	$strTR = $strTR."<td><input type='text' name='wavelength' id='wavelength'></td></tr>";
	$strTR = $strTR."<tr ><td>功率(mw)</td>";
	$strTR = $strTR."<td><input type='text' name='power' id='power'></td></tr>";
	$strTR = $strTR."<tr ><td>价格(￥)</td>";
	$strTR = $strTR."<td><input type='text' name='price' id='price'></td></tr>";
	$strTR = $strTR."<tr ><td>备注</td>";
	$strTR = $strTR."<td><input type='text' name='note' id='note' size=32></td></tr>";
	$strTR = $strTR."<tr ><td>产品分类</td>";
	$strTR = $strTR."<td><input type='text' name='type_id' id='type_id' ><br>1=绿光笔，2=蓝光笔，3=红光笔，4=模组管子，5=激光其他，6=显微镜，7=相机，8=办公</td></tr>";
	$strTR = $strTR."<tr ><td>排序ID</td>";
	$strTR = $strTR."<td><input type='text' name='order_id' id='order_id'><br>排在编号xxx的后面，默认排在最后面</td></tr>";
	$strTR = $strTR."<tr ><td>是否在用</td>";
	$strTR = $strTR."<td><input type='text' name='use_flag' id='use_flag' value=0><br>0=能用，1=不能用(不良产品)</td></tr>";
	$strTR = $strTR."<tr><td colspan=2 align=center>";
	$strTR = $strTR."<input type='button' value='新增' onclick='insertProduct()'>";
	$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
	$strTR = $strTR."</td></tr>";
	echo $strTR;
	
}else{	
	if(!$result->EOF){
		$strTR = "";
		$strTR = $strTR."<tr ><td>ID</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>产品图片</td>";
		$strTR = $strTR."<td><input type='text' name='image_name' id='image_name' value='".$result->fields['image_name']."'><img src='images/".$result->fields['image_name']."' width=50></td></tr>";
		$strTR = $strTR."<tr ><td>工厂名称</td>";
		$strTR = $strTR."<td><input type='text' name='factory_name' id='factory_name' value='".$result->fields['factory_name']."'></td></tr>";
		$strTR = $strTR."<tr ><td>波长(nm)</td>";
		$strTR = $strTR."<td><input type='text' name='wavelength' id='wavelength' value='".$result->fields['wavelength']."'></td></tr>";
		$strTR = $strTR."<tr ><td>功率(mw)</td>";
		$strTR = $strTR."<td><input type='text' name='power' id='power' value='".$result->fields['power']."'></td></tr>";
		$strTR = $strTR."<tr ><td>价格(￥)</td>";
		$strTR = $strTR."<td><input type='text' name='price' id='price' value='".$result->fields['price']."'></td></tr>";
		$strTR = $strTR."<tr ><td>备注</td>";
		$strTR = $strTR."<td><input type='text' name='note' id='note' size=32 value='".$result->fields['note']."'></td></tr>";
		$strTR = $strTR."<tr ><td>产品分类</td>";
		$strTR = $strTR."<td><input type='text' name='type_id' id='type_id' value='".$result->fields['type_id']."'><br>1=绿光笔，2=蓝光笔，3=红光笔，4=模组管子，5=激光其他，6=显微镜，7=相机，8=办公</td></tr>";
		$strTR = $strTR."<tr ><td>排序ID</td>";
		$strTR = $strTR."<td><input type='text' name='order_id' id='order_id' value='".$result->fields['order_id']."'><br>排在编号xxx的后面，默认排在最后面</td></tr>";
		$strTR = $strTR."<tr ><td>是否在用</td>";
		$strTR = $strTR."<td><input type='text' name='use_flag' id='use_flag' value='".$result->fields['use_flag']."'><br>0=能用，1=不能用(不良产品)</td></tr>";
		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='修改' onclick='updateProduct(".$id.")'>";
		$strTR = $strTR."<input type='button' value='删除' onclick='deleteProduct(".$id.")'>";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
		
	}
}
?>

</table>



</div>

<br><br>
<br><br>

</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="productId" id="productId" >
</form>
</body>




</html>

<script>
<?php
if($doFlag == "insertProduct"){
	echo "window.location.href = 'factoryList.php'";
}
?>

function updateProduct(id){
	document.getElementById('doFlag').value = 'updateProduct';
	document.getElementById('productId').value = id;
	document.inputForm.submit();
}

function insertProduct(){
	document.getElementById('doFlag').value = 'insertProduct';	
	document.inputForm.submit();
}

function toList(){
	window.location.href = "factoryList.php";
}

function deleteProduct(id){
	if(confirm("确认要删除？")){
		document.getElementById('doFlag').value = 'deleteProduct';
		document.getElementById('productId').value = id;
		document.inputForm.submit();
	}
}
</script>

